from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.http import HttpResponse

from metaweb import perform_upload, perform_write
from metaweb import get_domain_used, create_topic, create_topic_unconditional, link_topic

import json_custom as json
import sys
#import simplejson     # JSON encoding.
import urllib         # URI encoding.
import urllib2        # High-level URL content fetching.
import logging

def testFunction(request, companyName):
    logging.debug('testFunction');

def process_write_blurb(request, topicName):
    logging.debug('process_write_blurb\n');
    value = request.POST.__getitem__('e_value');
    logging.info('uploading blurb text: %s \n'  % value);
    article_id = request.POST.__getitem__('article_id');
    logging.debug(article_id+'\n');
        
    if request.POST.__getitem__('p_id') == '/topic/overview': 
        logging.debug('inside Post');
        logging.debug(value);
        logging.debug(article_id);
        logging.debug('\n\n Perform Upload \n\n');

        html = perform_upload(
                              value,
                              article_id,
                              True,
                              request
                              )
        logging.debug(html);
        obj = json.read(html);

        #logging.debug(repr(obj)); 
        #logging.debug(obj["status"]+'\n');
        #logging.debug(repr(obj["result"])+'\n');
        result = obj["result"];
        new_article_id = result["id"];
        #logging.debug('topic id = ' + topic_id +'\n');
        #logging.debug('old article id = ' + article_id +'\n');
        #logging.debug('new article id = ' + new_article_id +'\n');
        #logging.debug('\n\n Perform Create \n\n');
        #html_create = process_create_blurb(new_article_id, new_article_id);
        #logging.debug('\n\nhtml_create : \n');
        #logging.debug(html_create);
        #obj_create = json.read(html_create);
        #logging.debug('\n\nobject_create\n');
        #logging.debug(repr(obj_create));
        #logging.debug('\n\nobject_create: q1\n');
        #logging.debug(repr(obj_create["q1"])+'\n');
        #create_q1 = obj_create["q1"];
        #logging.debug('\n\nobject_create q1 _ result\n');
        #logging.debug(repr(create_q1["result"])+'\n');
        #create_result = create_q1["result"];
        #logging.debug('\n\nobject_create q1 _ result _ id\n');
        #logging.debug(repr(create_result["id"])+'\n');
        #create_article_id = create_result["id"];
        
        #create_result = create_q1["result"];
        #logging.debug('\n\n Perform Link \n\n');
        html3 = process_link_blurb(request.POST.__getitem__('topic_id'), new_article_id, request);
        #html3 = process_link_blurb(request.POST.__getitem__('topic_id'), create_article_id);
        #obj = read("{}");
        #self.assertEqual({}, obj);
        #logging.debug('\n\n End of Write_Blurb \n\n');
        
        return html;

def process_connect_blurb(request=None):
    logging.debug('inside Post-2');
    html2 = process_create_blurb('test', new_article_id);
    logging.debug('inside Post-3');
    html3 = process_link_blurb(request.POST.__getitem__('topic_id'), new_article_id, request);



def link_topics_through_property(request,topic2_id,newval_id):
    
    id = request.POST.__getitem__('id_rep') 
    
    topic_id = request.POST.__getitem__('topic_id')
    topic1_property = id
    tid = 'tid_'+id
    topic1_id = request.POST.__getitem__(tid)
    topic1_type = request.POST.__getitem__('topic1_type')
    topic2_type = request.POST.__getitem__('topic2_type')
    
    logging.debug('link_topics_through_property\n');
    logging.debug('topic1_id:'+topic1_id+'\n');
    logging.debug('topic1_type:'+topic1_type+'\n');
    logging.debug('topic2_id:'+topic2_id+'\n');
    logging.debug('topic2_type:'+topic2_type+'\n');
    logging.debug('topic_id:'+topic_id+'\n');
    logging.debug('newval_id:'+newval_id+'\n');
    
    html = perform_write(
                         'mql/link_topics_through_property.mql',
                         {
                            'topic1_id': topic1_id, #monoclonal antibody
                            'topic1_type': topic1_type, #/technologyclass
                            'topic1_property': topic1_property, #companies_involved_in_.....
                            'topic2_id': topic2_id,#e.g. this is company involved in name...amgen
                            'topic2_type': topic2_type,#/science technology_company
                            'unique_id': newval_id,
                            'topic_id': topic_id
                         },
                         True,
                         request
                         )
        
    return html;

def unlink_topics_through_property(request,topic2_id,delval_id):
    logging.debug('test_5');
    id = request.POST.__getitem__('id_rep') 
    topic1_property = id
    topic_id = request.POST.__getitem__('topic_id')
    tid = 'tid_'+id
    topic1_id = request.POST.__getitem__(tid)
    topic1_type = request.POST.__getitem__('topic1_type')
    topic2_type = request.POST.__getitem__('topic2_type')
    html = perform_write(
                         'mql/unlink_topics_through_property.mql',
                         {
                            'topic1_id': topic1_id, #monoclonal antibody
                            'topic1_type': topic1_type, #/technologyclass
                            'topic1_property': topic1_property, #companies_involved_in_.....
                            'topic2_id': topic2_id,#e.g. this is company involved in name...amgen
                            'topic2_type': topic2_type,#/science technology_company
                            'unique_id': delval_id,
                            'topic_id': topic_id
                         },
                         True,
                         request
                         )
        
    return html;



def process_create(request, topicName):
    logging.debug('process_create : ' + topicName);
    logging.debug('type = ' + request.POST.__getitem__('topic_type'));
    topic_type = request.POST.__getitem__('topic_type')
    topic_name = topicName.replace('/','')
    html = create_topic(topic_type, topic_name, "unless_exists", request)
    #html = perform_write(
    #                    'mql/create_topic.mql',
    #                    {
    #                       'topic_name': topicName.replace('/',''),
    #                       'topic_type': request.POST.__getitem__('topic_type'),
    #                    },
    #                    True
    #                    )
    return html;

def process_link_type(request):
    logging.debug('name = ' + request.POST.__getitem__('name'));
    html = perform_write(
                         'mql/link_type.mql',
                         {
                            'name': request.POST.__getitem__('name'),
                            'type_current': request.POST.__getitem__('type_current'),
                            'type_new': request.POST.__getitem__('type_new'),
                         },
                         True,
                         request
                         )
    return html;


def process_create_blurb(article_title, article_id, request=None):
    logging.debug('process_create_blurb : ' + article_id);
    html = perform_write(
                         'mql/create_article.mql',
                         {
                            'article_title': article_title,
                            'content_id': article_id,
                         },
                         True,
                         request
                         )
    return html;

def process_link_blurb(topic_id, article_id, request=None):
    logging.debug('process_create_blurb : ' + topic_id + ' : ' + article_id);
    html = perform_write(
                         'mql/link_article.mql',
                         {
                            'topic_id': topic_id,
                            'article_id': article_id,
                         },
                         True,
                         request
                         )
    return html;

